
$(document).ready(function()
{



    
  
    



})


var parse_bulk = function(data){
        var data = data.split('\n');
        var keyword = ''; 
        var ndata = {};
        for(var i=0;i<data.length;i++){  
            //this is where we go through each line of the config file
            //goal is to turn it into a dictionary of all the terms with data associated
            
            //first we get rid of the comments
            if(data[i].search('//') !=-1){var line=data[i].slice(0,data[i].search('//'))}
            else{var line = data[i]}
            if(!line){continue}
            
            if (line.charAt(0) == '#' && line.charAt(1)=='#') {
                keyword = line.slice(2,line.search(':')).toLowerCase()
                ndata[keyword]=[];
                continue
            }
            else if (line.charAt(0) == '#'){
                ndata[line.slice(1,line.search(':')).toLowerCase()]=line.slice(1+line.search(':'));
                continue
            }
            else{
                if(keyword){
                    ndata[keyword].push(line)
                }
            }
        }
        //confirm($.toJSON(ndata))
        return ndata
    }

var makeoptions = function(options){
        var htm = ''
        //confirm(options)
        for(var i=0;i<options.length;i++){ 
            htm += '<option value="'+options[i]+'">'+options[i]+'</option>';
        }
        return htm
    }



// TitleCase for strings
    String.prototype.TC = function() {
		var o_split = this.split(" ");
		//this probably isn't a complete list of words that shouldn't be capitalized
		var special_words = new Array('and', 'the', 'to', 'for', 'is', 'in', 'a', 'at', 'an', 'from', 'by', 'if', 'of');
		for (var i=0;i<o_split.length;i++){
			if (i == 0) {
				//always capitalize the first word
				o_split[i] = (o_split[i].substring(0,1)).toUpperCase() + o_split[i].substring(1);
			}
			else if(special_words.indexOf(o_split[i]) < 0) { 
			  	o_split[i] = (o_split[i].substring(0,1)).toUpperCase() + o_split[i].substring(1);
			}
		}
      return o_split.join(' ');
    };

// TitleCase for strings
    String.prototype.purge = function(str) {
        var txt = this;
        var len = txt.length;
        while(true){
            txt = txt.replace(str, "");
            if(len == txt.length){return txt}
            else{len = txt.length}
        }
    };
    
    
    var keys = function(o){
    //do the same thing as the Perl "keys" subroutine
        var accumulator = [];
        for (var propertyName in o) {
          accumulator.push(propertyName);
          }
        return accumulator;
    }
 
//get values instead of keys
    var values = function(o){
        var accumulator = [];
        for (var propertyName in o) {
          accumulator.push(o[propertyName]);
          }
        return accumulator;
    }
    
// Array Remove - By John Resig (MIT Licensed)
    Array.prototype.remove = function(from, to) {
      var rest = this.slice((to || from) + 1 || this.length);
      this.length = from < 0 ? this.length + from : from;
      return this.push.apply(this, rest);
    };

    Array.prototype.find = function(val) {
        for(var i=0;i<this.length;i++){  
            if (this[i]==val){return i}
        }
        return -1
    };
    
    function MDA(iRows,iCols) 
        { 
            var i; 
            var j; 
               var a = new Array(iRows); 
               for (i=0; i < iRows; i++) 
               { 
                   a[i] = new Array(iCols); 
                   for (j=0; j < iCols; j++) 
                   { 
                       a[i][j] = ""; 
                   } 
               } 
               return(a); 
        } 
        
        
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
    name =  name.replace(' ','_');
    value =  value.replace(' ','_');
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    name =  name.replace(' ','_');
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length).replace('_',' ');
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}  


    
    
    //Making a table from a data
//include separator if data is in text format, header is not ever seperated
var MakeTable = function(header,data,separator,rowfunc,cellfunc,sfx,sfxstart){
    if(!sfx){sfx='';sfxstart=100;}
    var ndata =  new Array();
    if(separator){
        for(var i=0;i<data.length;i++){
            ndata.push(data[i].split(separator));
        }
    }
    else{ndata = data}
    
    var tab = $('<table class="tablesorter teachertable" border="0" cellpadding="0" cellspacing="1"></table>');
    var head = $('<thead></thead>');
    var hrow = $('<tr></tr>');
    for(var i=0;i<header.length;i++){
        $('<th>'+header[i]+'</th>').appendTo(hrow);    
    }
    hrow.appendTo(head);
    head.appendTo(tab);
    var rows = new Array;
    for(var i=0;i<ndata.length;i++){
        var row = $('<tr id="'+ndata[i][0]+'" id2="'+ndata[i][1]+'"></tr>');
        for(var j=0;j<ndata[i].length;j++){ 
            if(j<sfxstart){var sufx = '';}
            else{var sufx = sfx;}
            var cell = $('<td sname="'+ndata[i][1]+'" tname="'+ndata[i][0]+'" cnum="'+j+'">'+ndata[i][j]+sufx+'</td>');
            cell.appendTo(row);
            cell.click(function(){cellfunc($(this).attr('sname'),$(this).attr('tname'),$(this).attr('cnum'))})
        }
         row.appendTo(tab);
         row.click(function(){rowfunc($(this).attr('id'),$(this).attr('id2'))})
    }
    
    return tab
}


//stuff for using the database
var fetchall = function(rs){
        var data = [];
        var count = 0;
        var go = true;
        while (rs.isValidRow()) {
            if(go){
                go=false;
                count = rs.fieldCount();
                var head = [];
                for(var i=0;i<count;i++){
                    head.push(rs.fieldName(i))
                }
                data.push(head);
            }
            var temp = [];
            for(var i=0;i<count;i++){
                temp.push(rs.field(i))
            }
            data.push(temp);
          rs.next();
        }
        rs.close();
        
        return data
    }
    
    var dict = function(keys,values){
        var d = {};
        for(var i=0;i<keys.length;i++){
            d[keys[i]] = values[i];
        }
        return d
    }
    
    
    var fetchdict = function(rs){
        var d = fetchall(rs);
        var data = [];
        for(var i=1;i<d.length;i++){
            data.push(dict(d[0],d[i]))
        }
        
        
        return data
    }
